/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui;

import easybooking.FlightManagement;
import easybooking.data.Airline;
import easybooking.data.AirlineImpl;
import easybooking.data.Airport;
import easybooking.data.AirportImpl;
import easybooking.data.Customer;
import easybooking.data.CustomerImpl;
import easybooking.data.Plane;
import easybooking.data.PlaneImpl;
import easybooking.data.Type;
import easybooking.data.TypeImpl;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Tim Schroeder
 */
public class EBInterfaceClient {

    FlightManagement management = null;
    private Vector cv = new Vector();
    private Vector tv = new Vector();
    private Vector airpv = new Vector();
    private Vector pv = new Vector();
    private Vector airlv = new Vector();
    private Vector fv = new Vector();
    private String hostname = null;

    public EBInterfaceClient() {
        this.createDummyCustomers();
        this.createDummyTypes();
        this.createDummyAirports();
        this.createDummyPlanes();
        this.createDummyAirlines();
    }

    /**
     * Initialises the conenction to the remote-Object "FlightManagement1" by looking up the RMI-Registry
     * @param hostname
     * @return a boolean determining if the connection was successfull
     */
    public boolean initServerConnection(String hostname) {
        this.hostname = hostname;
        String rmiRegistryServer = System.getProperty("java.rmi.registry.hostname", this.hostname);
        try {
            // Zunächst benötigen wir eine Verbindung mit einem Flight Management.
            management = (FlightManagement) Naming.lookup("rmi://" + rmiRegistryServer + ":1099/FlightManagement1");

            /*
             * Wenn wir hier ankommen, dann besteht eine Verbinung zum Server.
             * Wäre die Verbindung nicht zustande gekommen, so wäre eine
             * Exception ausgeworfen worden und das Programm wäre in den
             * entsprechenden Catch-Block (s.u.) gesprungen.
             */
            System.out.println("Verbindung hergestellt...");


        } catch (MalformedURLException murle) {
            System.out.println("MalformedURLException");
            System.out.println(murle);
            return false;
        } catch (RemoteException re) {
            System.out.println("RemoteException");
            System.out.println(re);
            return false;
        } catch (NotBoundException e) {
            System.out.println("NotBoundException");
            System.out.println(e);
            return false;
        }
        return true;
    }

    private void createDummyCustomers() {
        try {
            Customer c1 = new CustomerImpl();
            c1.setCustomerId(1);
            c1.setFirstname("Tim");
            c1.setLastname("Schroeder");
            c1.setStreet("Bachstraße");
            c1.setZip(92345);
            c1.setCity("Ulm");
            Customer c2 = new CustomerImpl();
            c2.setCustomerId(2);
            c2.setFirstname("Andrea");
            c2.setLastname("Schaefer");
            c2.setStreet("Gartenweinberge");
            c2.setZip(12345);
            c2.setCity("Ludwigsburg");
            Customer c3 = new CustomerImpl();
            c3.setCustomerId(3);
            c3.setFirstname("Benjamin");
            c3.setLastname("Blümchen");
            c3.setStreet("Zoostrase");
            c3.setZip(32865);
            c3.setCity("Neustadt");
            Customer c4 = new CustomerImpl();
            c4.setCustomerId(4);
            c4.setFirstname("Donald");
            c4.setLastname("Duck");
            c4.setStreet("Ganstraße");
            c4.setZip(72865);
            c4.setCity("Entenhausen");
            cv.add(c1);
            cv.add(c2);
            cv.add(c3);
            cv.add(c4);
        } catch (RemoteException ex) {
            Logger.getLogger(EBInterfaceClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Gets all customers from the management object
     * @return A vector containing customers.
     */
    public Vector getAllCustomers() {
        return cv;

    }

    /**
     *
     * @param firstName
     * @param lastName
     * @param street
     * @param zip
     * @param city
     */
    public void createCustomer(String firstName, String lastName, String street, String zip, String city) {
        try {
            CustomerImpl c3 = new CustomerImpl();
            Customer lastCustomer = (Customer) cv.lastElement();
            c3.setCustomerId(lastCustomer.getCustomerId() + 1);
            c3.setFirstname(firstName);
            c3.setLastname(lastName);
            c3.setStreet(street);
            c3.setZip(Integer.parseInt(zip));
            c3.setCity(city);
            cv.add(c3);
        } catch (RemoteException ex) {
            Logger.getLogger(EBInterfaceClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     *
     * @param c
     */
    public void deleteCustomer(Customer c) {
        cv.remove(c);
    }

    /**
     *
     * @param firstName
     * @param lastName
     * @param street
     * @param zip
     * @param city
     * @param c
     */
    public void updateCustomer(String firstName, String lastName, String street, String zip, String city, Customer c) {
        try {
            CustomerImpl c4 = new CustomerImpl();
            c4.setCustomerId(c.getCustomerId());
            c4.setFirstname(firstName);
            c4.setLastname(lastName);
            c4.setStreet(street);
            c4.setZip(Integer.parseInt(zip));
            c4.setCity(city);
            cv.set(cv.indexOf(c), c4);
        } catch (RemoteException ex) {
            Logger.getLogger(EBInterfaceClient.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void createDummyTypes() {
        try {
            Type t1 = new TypeImpl();
            t1.setTypeId(1);
            t1.setName("Boing 747");
            t1.setRows(20);
            t1.setColumns(6);
            Type t2 = new TypeImpl();
            t2.setTypeId(2);
            t2.setName("Boing 737");
            t2.setRows(10);
            t2.setColumns(5);
            Type t3 = new TypeImpl();
            t3.setTypeId(3);
            t3.setName("Airbus A380");
            t3.setRows(50);
            t3.setColumns(8);
            Type t4 = new TypeImpl();
            t4.setTypeId(4);
            t4.setName("DC-10");
            t4.setRows(10);
            t4.setColumns(4);
            tv.add(t1);
            tv.add(t2);
            tv.add(t3);
            tv.add(t4);
        } catch (RemoteException ex) {
            Logger.getLogger(EBInterfaceClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     *
     * @return
     */
    public Vector getAllTypes() {

        return tv;
    }

    /**
     *
     * @param name
     * @param rows
     * @param columns
     */
    public void createType(String name, String rows, String columns) {
        try {
            TypeImpl t3 = new TypeImpl();
            Type lastType = (Type) tv.lastElement();
            t3.setTypeId(lastType.getTypeId() + 1);
            t3.setName(name);
            t3.setRows(Integer.parseInt(rows));
            t3.setColumns(Integer.parseInt(columns));
            tv.add(t3);
        } catch (RemoteException ex) {
            Logger.getLogger(EBInterfaceClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     *
     * @param name
     * @param rows
     * @param columns
     * @param t
     */
    public void updateType(String name, String rows, String columns, Type t) {
        try {
            TypeImpl t4 = new TypeImpl();
            t4.setTypeId(t.getTypeId());
            t4.setName(name);
            t4.setRows(Integer.parseInt(rows));
            t4.setColumns(Integer.parseInt(columns));
            tv.set(tv.indexOf(t), t4);
        } catch (RemoteException ex) {
            Logger.getLogger(EBInterfaceClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     *
     * @param t
     */
    public void deleteType(Type t) {
        tv.remove(t);
    }

    /**
     *
     * @param id
     * @return
     */
    public Type findTypeByKey(int id) {
        Iterator it = tv.iterator();
        while (it.hasNext()) {
            try {
                Type t = (Type) it.next();
                if (t.getTypeId() == id) {
                    return t;
                }
            } catch (RemoteException ex) {
                Logger.getLogger(EBInterfaceClient.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }

    private void createDummyAirports() {
        try {
            Airport airp1 = new AirportImpl();
            airp1.setAirportId(1);
            airp1.setName("John F. Kenedy International Airport");
            airp1.setInitials("JFK");
            airp1.setCity("New York");
            Airport airp2 = new AirportImpl();
            airp2.setAirportId(2);
            airp2.setName("Frankfurt Airport");
            airp2.setInitials("FRA");
            airp2.setCity("Frankfurt");
            Airport airp3 = new AirportImpl();
            airp3.setAirportId(3);
            airp3.setName("Berlin Tegel Airport");
            airp3.setInitials("TXL");
            airp3.setCity("Berlin");
            Airport airp4 = new AirportImpl();
            airp4.setAirportId(4);
            airp4.setName("Paris Charles de Gaulle Airport");
            airp4.setInitials("CDG");
            airp4.setCity("Paris");
            airpv.add(airp1);
            airpv.add(airp2);
            airpv.add(airp3);
            airpv.add(airp4);
        } catch (RemoteException ex) {
            Logger.getLogger(EBInterfaceClient.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    /**
     *
     * @return
     */
    public Vector getAllAirports() {
        Vector v = new Vector();
        try {
            v = management.getAllAirports();
            return v;
        } catch (RemoteException ex) {
            Logger.getLogger(EBInterfaceClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        return v;

    }

    /**
     *
     * @param name
     * @param initials
     * @param city
     */
    public void createAirport(String name, String initials, String city) {
        Airport airp = null;

        if (management == null) {
            initServerConnection(hostname);
        }
        // Die Bankverwaltung bitten wir, einen neuen Kunden anzulegen.
        try {
            airp = management.createAirport(name, initials, city);

            /*
             * Wenn es darauf hin eine solchen Kunden gibt (daran zu erkenn,
             * dass c nicht leer ist), dann lesen wir nun einige Informationen
             * über den Kunden aus und setzen das ID-Feld.
             */
            if (airp != null) {
                // Einfache Ausgabe der Daten des neuen Kundenobjekts
                System.out.println("Neuer Airport: " + "Airport.-ID.: " + airp.getAirportId() + ", " + airp.getName() + ", " + airp.getInitials() + ", " + airp.getCity());

            } else {
                System.out.println("Kein Airport angelegt bzw. verfügbar.");
            }
        } catch (RemoteException re) {
            System.out.println("RemoteException");
            System.out.println(re);

        }
        //return airp;
    }

    /**
     *
     * @param name
     * @param initials
     * @param city
     * @param airp
     */
    public void updateAirport(String name, String initials, String city, Airport airp) {
        if (management == null) {
            initServerConnection(hostname);
        }
        try {
            airp.setName(name);
            airp.setInitials(initials);
            airp.setCity(city);
            management.save(airp);
        } catch (RemoteException re) {
            System.out.println("RemoteException");
            System.out.println(re);

        }

    }

    /**
     *
     * @param airp
     */
    public void deleteAirport(Airport airp) {
        airpv.remove(airp);
    }

    private void createDummyPlanes() {
        try {
            Plane p1 = new PlaneImpl();
            p1.setPlaneId(1);
            p1.setName("Landshut");
            p1.setTypeId(1);
            p1.setAirlineId(1);
            Plane p2 = new PlaneImpl();
            p2.setPlaneId(2);
            p2.setName("Stuttgart");
            p2.setTypeId(2);
            p2.setAirlineId(2);
            pv.add(p1);
            pv.add(p2);
        } catch (RemoteException ex) {
            Logger.getLogger(EBInterfaceClient.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     *
     * @return
     */
    public Vector getAllPlanes() {
        return pv;
    }

    /**
     *
     * @param name
     * @param typeId
     * @param airlineId
     */
    public void createPlane(String name, int typeId, int airlineId) {
        try {
            PlaneImpl p3 = new PlaneImpl();
            Plane lastPlane = (Plane) pv.lastElement();
            p3.setPlaneId(lastPlane.getPlaneId() + 1);
            p3.setName(name);
            p3.setTypeId(typeId);
            p3.setAirlineId(airlineId);
            pv.add(p3);
        } catch (RemoteException ex) {
            Logger.getLogger(EBInterfaceClient.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     *
     * @param name
     * @param typeId
     * @param airlineId
     * @param p
     */
    public void updatePlane(String name, int typeId, int airlineId, Plane p) {
        try {
            PlaneImpl p4 = new PlaneImpl();
            p4.setPlaneId(p.getPlaneId());
            p4.setName(name);
            p4.setTypeId(typeId);
            p4.setAirlineId(airlineId);
            pv.set(pv.indexOf(p), p4);
        } catch (RemoteException ex) {
            Logger.getLogger(EBInterfaceClient.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     *
     * @param p
     */
    public void deletePlane(Plane p) {
        pv.remove(p);
    }

    private void createDummyAirlines() {
        try {
            Airline airl1 = new AirlineImpl();
            airl1.setAirlineId(1);
            airl1.setName("Lufthansa");
            airl1.setInitials("LHX");
            Airline airl2 = new AirlineImpl();
            airl2.setAirlineId(2);
            airl2.setName("British Airways");
            airl2.setInitials("BA");
            airlv.add(airl1);
            airlv.add(airl2);
        } catch (RemoteException ex) {
            Logger.getLogger(EBInterfaceClient.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     *
     * @return
     */
    public Vector getAllAirlines() {
        Vector v = new Vector();
        try {
            v = management.getAllAirlines();
            return v;
        } catch (RemoteException ex) {
            Logger.getLogger(EBInterfaceClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        return v;

    }

    /**
     *
     * @param id
     * @return
     */
    public Airline findAirlineByKey(int id) {
        Iterator it = airlv.iterator();
        while (it.hasNext()) {
            try {
                Airline airl = (Airline) it.next();
                if (airl.getAirlineId() == id) {
                    return airl;
                }
            } catch (RemoteException ex) {
                Logger.getLogger(EBInterfaceClient.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;

    }

    /**
     *
     * @param name
     * @param initials
     */
    public void createAirline(String name, String initials) {
        Airline airl = null;

        if (management == null) {
            initServerConnection(hostname);
        }
        // Die Bankverwaltung bitten wir, einen neuen Kunden anzulegen.
        try {
            airl = management.createAirline(name, initials);

            /*
             * Wenn es darauf hin eine solchen Kunden gibt (daran zu erkenn,
             * dass c nicht leer ist), dann lesen wir nun einige Informationen
             * über den Kunden aus und setzen das ID-Feld.
             */
            if (airl != null) {
                // Einfache Ausgabe der Daten des neuen Kundenobjekts
                System.out.println("Neue Airline: " + "Airline.-ID.: " + airl.getAirlineId() + ", " + airl.getName() + ", " + airl.getInitials());

            } else {
                System.out.println("Keine Airline angelegt bzw. verfügbar.");
            }
        } catch (RemoteException re) {
            System.out.println("RemoteException");
            System.out.println(re);

        }
        //return airp;

    }

    /**
     *
     * @param name
     * @param initials
     * @param airl
     */
    public void updateAirline(String name, String initials, Airline airl) {
        try {
            AirlineImpl airl4 = new AirlineImpl();
            airl4.setAirlineId(airl.getAirlineId());
            airl4.setName(name);
            airl4.setInitials(initials);
            airlv.set(airlv.indexOf(airl), airl4);
        } catch (RemoteException ex) {
            Logger.getLogger(EBInterfaceClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     *
     * @param airl
     */
    public void deleteAirline(Airline airl) {
        airlv.remove(airl);
    }

    /**
     *
     * @return
     */
    public Vector getAllFlights() {
        return fv;

    }
}
